package Framework;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.PopupMenu;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;

public class pnl_Scrollable extends pnl_Ancestor implements ActionListener, MouseListener, MouseMotionListener{
		static  String CLASS = "pnl_Scrollable";
	
		protected ImageIcon 		backgroundImage;   
	    protected boolean			ib_ScrollHorizontal;
	    protected Rectangle			i_PanelSize;
	    protected Rectangle			i_InternalSize;
	    public	  pnl_Scrollable_InternalFrame 		i_InternalFrame;
	    protected Point				ir_PressedPoint;
	    private		Component		comp_ReturnVal;
	    
	    boolean ib_EventShouldntOccur = false; // This is to prevent 'clicking' on an object while dragging.
	    
	    public pnl_Scrollable(Rectangle a_PanelSize, Rectangle a_InternalSize, boolean ab_ScrollHorizontal) {
	 	   super();
	 	   // Enforcing restrictions on panel size.
	 	   if (a_PanelSize.y < ConfigOpts.SIZE_TOPBAR_HEIGHT){
	 		   a_PanelSize.height += a_PanelSize.y - ConfigOpts.SIZE_TOPBAR_HEIGHT;
	 		   a_PanelSize.y = ConfigOpts.SIZE_TOPBAR_HEIGHT;
	 	   }
	 	   if (a_PanelSize.y + a_PanelSize.height > (600 - ConfigOpts.SIZE_BOTTOMBAR_HEIGHT)){
	 		   a_PanelSize.height = (600 - (a_PanelSize.y + ConfigOpts.SIZE_BOTTOMBAR_HEIGHT));
	 		   System.out.println("SETTING TO: " + a_PanelSize.height);
	 	   }; 
	 	   
	 	   this.setBounds(a_PanelSize);
	 	   this.setLayout(null);
	 	   
	 	   ib_ScrollHorizontal 		= ab_ScrollHorizontal;
	 	   i_InternalSize 			= a_InternalSize;
	 	   i_PanelSize 				= a_PanelSize;
	 	   
	 	   i_InternalFrame = new pnl_Scrollable_InternalFrame();
	 	   
	 	   setInternalFrameSize(a_InternalSize);
	 	   i_InternalFrame.setBounds(i_InternalSize);

	 	   i_InternalFrame.setBackgroundImage(ConfigOpts.IMAGE_BACKGROUND_SCROLLABLE);
	 	   i_InternalFrame.addMouseListener(this);
	 	   i_InternalFrame.addMouseMotionListener(this);
	 	   i_InternalFrame.setBackgroundImage("img/padPaper.png");
	 	   i_InternalFrame.setLayout(null);
	 	   
	 	   this.setBackgroundImage("img/404_fffuuu.png");
	 	   super.add(i_InternalFrame);
		}
	    
	    public void setInternalFrameSize(Rectangle a_InternalSize){
	    	System.out.println("Set internal frfame size");
	    	if (a_InternalSize.height < i_PanelSize.height){
		 		   a_InternalSize.height = i_PanelSize.height;
		 	   }
		 	   if (a_InternalSize.width < i_PanelSize.width){
		 		   a_InternalSize.width = i_PanelSize.width;
		 	   }
		 	   
	    	i_InternalSize = a_InternalSize;
	    }
	    public void clear(){
	    	i_InternalFrame.removeAll();
	    	i_InternalFrame.setBounds(0,0,i_InternalFrame.getWidth(), (int) i_PanelSize.getHeight());
	    }
	    // Overriding methods to access child. Hopefully nothing else needs to be done (ie get_bounds etc)...
	    public void add(PopupMenu popup){ i_InternalFrame.add(popup);}
	    
	    public void add(Component component, Object constraints){ 
			i_InternalFrame.add(component, constraints);
			component.addMouseListener(this);
			component.addMouseMotionListener(this);
			validate();
		}
	    
	    public void add(Component component, Object constraints, int index){ 
	    	i_InternalFrame.add(component, constraints, index);
	    	validate();
	    }
	    
	    public Component add(Component component){ 
			component.addMouseListener(this);
			component.addMouseMotionListener(this);
			comp_ReturnVal = i_InternalFrame.add(component);
			validate();
			return comp_ReturnVal;
		}
	    
	    public Component add(Component component, int index){
	    	component.addMouseListener(this);
			comp_ReturnVal = i_InternalFrame.add(component, index);;
			validate();
			return comp_ReturnVal;
	    }
	    
	    public Component add(String name, Component component){
	    	component.addMouseListener(this);
			comp_ReturnVal = i_InternalFrame.add(name, component);
			validate();
			return comp_ReturnVal;
	    }
	    public void validate(){
	    	super.validate();
	    	i_InternalFrame.validate();
	    	this.repaint();
	    	i_InternalFrame.repaint();
	    }
		public void reset(){}
		
		public boolean equals(Object y){
			// Ok, this is dirty. Instead of checking if the event should occur, 
			// just returning false on the equals method. 
			if (ib_EventShouldntOccur == true) {
				return false;
			} else { 
				return i_InternalFrame.equals(y);
			}
		}


		public void mousePressed(MouseEvent arg0) {
			ir_PressedPoint = arg0.getPoint();
		}

		

		public void mouseDragged(MouseEvent arg0) {
			System.out.println();
			if (arg0.getModifiersEx() == 1024){
				Rectangle 	lr_InternalFramePoint 	= new Rectangle();
				Point		li_DistanceFromClicked 	= arg0.getPoint();
				int			y_Dist = (arg0.getPoint().y - ir_PressedPoint.y); 
				int			x_Dist = (arg0.getPoint().x - ir_PressedPoint.x);
				
				// Rectangle.clone() causing issues. Could take as reference but feelsbatman.png 
				lr_InternalFramePoint.x 		= i_InternalSize.x;
				lr_InternalFramePoint.height 	= i_InternalSize.height;
				lr_InternalFramePoint.width 	= i_InternalSize.width;
				lr_InternalFramePoint.y 		= i_InternalFrame.getBounds().y + y_Dist;	//i_InternalSize.y - li_DistanceFromClicked.y;
				
				if (lr_InternalFramePoint.y > 0){
					lr_InternalFramePoint.y = 0;	
				}
				if (lr_InternalFramePoint.y * -1 > lr_InternalFramePoint.height - i_PanelSize.height){
					//lr_InternalFramePoint.y = lr_InternalFramePoint.height - i_PanelSize.height; 
					lr_InternalFramePoint.y = (lr_InternalFramePoint.height - i_PanelSize.height) * -1;
				}
				
				if (ib_ScrollHorizontal){
					lr_InternalFramePoint.x 		= i_InternalSize.getBounds().x + x_Dist;
					if (lr_InternalFramePoint.x > 0){
						lr_InternalFramePoint.x = 0;	
					}
					if (lr_InternalFramePoint.x * -1 > lr_InternalFramePoint.width - i_PanelSize.width){ 
						lr_InternalFramePoint.x = (lr_InternalFramePoint.width - i_PanelSize.width) * -1;
					}
					
				}
				
				i_InternalFrame.setBounds(lr_InternalFramePoint);
				
				validate();
				
				if (y_Dist > 50) {
					ib_EventShouldntOccur = true;
					System.out.println(ib_EventShouldntOccur );
				} else {
					ib_EventShouldntOccur = false;
					System.out.println(ib_EventShouldntOccur );
				}
			}
		}

		public void mouseReleased(MouseEvent arg0) {}
		public void mouseMoved(MouseEvent arg0) { }
		public void mouseEntered(MouseEvent arg0) { }
		public void mouseExited(MouseEvent arg0) {}
		public void mouseClicked(MouseEvent arg0) {}
	 }  
